﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Services;
using ZLCMS.Business.TranferParam;
using ZLCMS.Business.Util;
using AjaxControlToolkit;
using Newtonsoft.Json;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.AJAXServer.Param
{
    /// <summary>
    /// 参数相关的服务
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class ParamAJAXServer : System.Web.Services.WebService
    {
        class DropDownValue
        {
            //用户未选择内容，下拉框的值
            public const string NoSelect = "-1";

            //下拉框无可用值，下拉框的值
            public const string Empty = "-2";
        }

        [WebMethod]
        public CascadingDropDownNameValue[] GetValueList(string knownCategoryValues, string category)
        {
            string parentValue;

            // 删除最后的‘;’号
            char[] charsToTrim = { ';' };
            knownCategoryValues = knownCategoryValues.TrimEnd(charsToTrim);

            //获取最后一个‘:’的位置，并获取字符串，该值为父下拉框的值
            int lastIndex = knownCategoryValues.LastIndexOf(':');
            if (lastIndex > 0)
            {
                parentValue = knownCategoryValues.Substring(lastIndex + 1);
            }
            else
            {
                parentValue = DropDownValue.Empty;
            }

            category = category.Replace("%?%", parentValue);


            ParamRequest req = JsonConvert.DeserializeObject<ParamRequest>(category);

            List<CascadingDropDownNameValue> returnList = new List<CascadingDropDownNameValue>();

            List<ParamKeyValuePair> pairList = ParamKit.GetValueList(req.Group, req.filterList);

            foreach (ParamKeyValuePair pair in pairList)
            {
                returnList.Add(new CascadingDropDownNameValue(pair.Value.ToString(), pair.Key));
            }

            return returnList.ToArray();
        }
    }
}
